package com.sy.module_clockin.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.DialogFragment
import com.sy.module_clockin.R
import com.sy.module_clockin.annotation.ClockInType
import com.sy.module_clockin.databinding.ModuleClockinDialogSaveSuccessBinding
import com.sy.module_clockin.recycler.dp2px
import com.sy.module_clockin.recycler.setOnFastClickListener
import java.util.*

class ClockInSuccessDialog(@ClockInType val clockInType: Int) : DialogFragment() {

    private var binding: ModuleClockinDialogSaveSuccessBinding? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = ModuleClockinDialogSaveSuccessBinding.inflate(inflater, container, false)
        return binding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.apply {
            setStyle(STYLE_NO_TITLE, R.style.StyleBaseDialog)
            dialog?.apply {
                setCanceledOnTouchOutside(true)
                window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                window?.attributes?.apply {
                    width = WindowManager.LayoutParams.MATCH_PARENT
                    height = WindowManager.LayoutParams.WRAP_CONTENT
                    window?.attributes = this
                }
            }

            clContainer.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                marginStart = requireContext().dp2px(54)
                marginEnd = marginStart
            }


            when (clockInType) {
                ClockInType.CLOCKIN_TYPE_GET_UP_EARLY -> {
                    btnClockIn.setBackgroundResource(R.drawable.module_clockin_round_blue)
                    clContainer.setBackgroundResource(R.mipmap.module_clockin_successful_bg_01)
                }

                ClockInType.CLOCKIN_TYPE_MOVEMENT -> {
                    btnClockIn.setBackgroundResource(R.drawable.module_clockin_round_yellow)
                    clContainer.setBackgroundResource(R.mipmap.module_clockin_successful_bg_02)
                }

                ClockInType.CLOCKIN_TYPE_DRINK_WATER -> {
                    btnClockIn.setBackgroundResource(R.drawable.module_clockin_round_cyan)
                    clContainer.setBackgroundResource(R.mipmap.module_clockin_successful_bg_03)
                }

                ClockInType.CLOCKIN_TYPE_READING -> {
                    btnClockIn.setBackgroundResource(R.drawable.module_clockin_round_pink)
                    clContainer.setBackgroundResource(R.mipmap.module_clockin_successful_bg_04)
                }
            }

            btnClockIn.setOnFastClickListener {
                this@ClockInSuccessDialog.dismiss()
            }

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        binding = null
    }
}